package 二叉树;

import Utils.TreeNode;

/**
 * @author TTDB
 * @create 2025/9/18
 */
public class maxPathSum {
    private int ans;
     public int maxPathSum(TreeNode root){
        dfs(root);
        return ans;
     }
     private int dfs(TreeNode root){
         if(root == null) return 0;
         int left = Math.max(dfs(root.left), 0);
         int right = Math.max(dfs(root.right), 0);
         ans = Math.max(ans, left + right + root.val);
         return Math.max(left, right) + root.val;//作为子链只能二选一
     }
}
